#include <stdlib.h>
#include <iostream>

using namespace std;

static void my_exit1(void);
static void my_exit2(void);

int main(void)
{
  try {
      if (atexit(my_exit2) != 0) {
          throw "can't register my_exit2";
      }    
  } catch (const char *err_msg) {
      cout << err_msg << endl;
  }

  try {
      if (atexit(my_exit1) != 0 ){
          throw "can't register my_exit1";
      }    
  } catch (const char *err_msg) {
      cout << err_msg << endl;
  }

  try {
      if (atexit(my_exit1) != 0 ){
          throw "can't register my_exit1";
      }    
  } catch (const char *err_msg) {
      cout << err_msg << endl;
  }
  cout << "main is done\n";
  return 0;
}

static void my_exit1(void)
{
    cout << "First exit handler\n";
}

static void my_exit2(void)
{
    cout << "Second exit handler\n";
}
